package cn.icyzx.dao;


/**
 * Created by 郑凯 on 2016/7/16.
 */

import cn.icyzx.models.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Repository;
import cn.icyzx.util.StaticConstUtil;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Properties;


@Repository("sendMail")
public class SendMail {

    private static Logger logger = Logger.getLogger(SendMail.class);

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    private static String email;

    static {
        Properties porperties = new Properties();
        try {
            porperties.load(SendMail.class.getClassLoader().getResourceAsStream("email.properties"));
            email = porperties.getProperty("email");
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("can't initialize email name");
        }
    }

    /**
     * sendMailToActive
     *
     * @param user 根据传入的user发送email激活用户
     * @return boolean 返回是否发送成功
     */
    public boolean sendMailToActive(User user) {

        try {
            MimeMessage msg = javaMailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(msg, true, "utf-8");
            message.setTo(user.getEmail());
            message.setFrom(email);
            message.setSubject("创业中心用户激活");
            String content = "<html><body>你将可以在这里和大家分享你的最新发现，讨论解决各种技术问题，寻找合作伙伴，展示你的作品，任意的奇思妙想，及更多等待你去发现的惊喜。<br/>感谢你支持我们的网站，<a href='" + StaticConstUtil.SERVER_NAME + "/activeUser?uid=" + user.getUid() + "'>请点开次连接，激活你的账号~</a></body></html>";
            message.setText(content, true);
            javaMailSender.send(msg);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.warn("给" + user.getUsername() + "发送的激活邮件失败");
            return false;
        } catch (MailException e) {
            e.printStackTrace();
            logger.warn("给" + user.getUsername() + "发送的激活邮件失败");
            return false;
        }

    }

    /**
     * sendEmailToFindPws
     *
     * @param user 根据user发送邮件找回密码
     * @return boolean
     */
    public boolean sendEmailToFindPws(User user) {

        try {
            MimeMessage msg = javaMailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(msg, true, "utf-8");
            message.setTo(user.getEmail());
            message.setFrom(email);
            message.setSubject("创业中心用户找回密码");
            String content = "<html><body>您在创业中心的账号正在找回密码，如不是本人操作，请勿于理会<br/>如要找回密码，请点击下面链接<a href='" + StaticConstUtil.SERVER_NAME + "/reset-psw?findPsw=" + user.getFindPsw() + "&uid=" + user.getUid() + "'>请点开次连接，重置你的密码~</a></body></html>";
            message.setText(content, true);
            javaMailSender.send(msg);

            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            logger.warn("给" + user.getUsername() + "发送的找回密码邮件失败");
            return false;
        } catch (MailException e) {
            e.printStackTrace();
            logger.warn("给" + user.getUsername() + "发送的找回密码邮件你失败");
            return false;
        }
    }

}